'use strict';

class UserConnector {
  constructor(ctx) {
    this.ctx = ctx;
    this.proxy = this.ctx.app.model.User;
  }
  async fetchById(id) {
    const res = await this.proxy.findOne({ where: { id } });
    return res.toJSON();
  }
  async fetchByList({ limit, page }) {
    return {
      data: await this.proxy.findAll({ limit, offset: (page - 1) * limit }),
      total: await this.proxy.count(),
    };
  }
  async fetchUpdate({ id, data }) {
    await this.proxy.update(data, {
      where: { id },
    });
    return await this.fetchById(id);
  }
  async fetchCreate({ data }) {
    const res = await this.proxy.create(data);
    return res.toJSON();
  }
}

module.exports = UserConnector;
